﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel;
using System.ServiceModel.Web;
using ApiPrototype.Handlers;
using ApiPrototype.Models;
using System.Collections.Specialized;
using System.Net.Http;

namespace ApiPrototype.APIs
{
    [ServiceContract]
    public class LoginApi
    {
        [WebInvoke(Method = "POST"), ContentParam(ParamName = "creds", ParamType = typeof(CredentialsDto))]
        public ApiResponse<LoginDto> Post(CredentialsDto creds, iphlEntities ctx, HttpRequestMessage request)
        {
            LoginDto result = new LoginDto() { Success = false };
            
            if (creds == null || creds.StudentId == 0 || string.IsNullOrEmpty(creds.Password))
            {
                result.Message = "Invalid credentials. StudentId must be greater than 0. Password can't be empty.";
                return new ApiResponse<LoginDto>(result);
            }
            
            result.Success = (ctx.STUDENT.Where(s => s.STU_ID == creds.StudentId && s.STU_WACHTWOORD.Equals(creds.Password)).Count() == 1);
            if (!result.Success)
            {
                result.Message = "Invalid combination of StudentId and Password.";
            }

            return new ApiResponse<LoginDto>(result);
        }

        [WebGet]
        public ApiResponse<LoginDto> Get(iphlEntities ctx)
        {
            LoginDto result = new LoginDto() { Success = false };
            result.Message = "Invalid combination of StudentId and Password.";

            return new ApiResponse<LoginDto>(result);
        }

    }
}